library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.4
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(maps)
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
library(ggplot2)
library(viridis)
## Loading required package: viridisLite
library(ggthemes)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gganimate)
library(countrycode)
happiness_2015 <- read.csv("https://raw.githubusercontent.com/Reed-Statistics/math241S20PostGrp4/master/2015.csv?token=ANDSWNMB7AQIJ672ORMVKL26OJ3H6")
happiness_2016 <- read.csv("https://raw.githubusercontent.com/Reed-Statistics/math241S20PostGrp4/master/2016.csv?token=ANDSWNPLYRU2KWDDIGFKSCS6OJ3JU")
happiness_2017 <- read.csv("https://raw.githubusercontent.com/Reed-Statistics/math241S20PostGrp4/master/2017.csv?token=ANDSWNKJG7RFYHRUAMITURC6OJ3M4")
happiness_2018 <- read.csv("https://raw.githubusercontent.com/Reed-Statistics/math241S20PostGrp4/master/2018.csv?token=ANDSWNNU3I7TF5JQKPQXAJK6OJ3OU")
happiness_2019 <- read.csv("https://raw.githubusercontent.com/Reed-Statistics/math241S20PostGrp4/master/2019.csv?token=ANDSWNNCDOIVOFWIVMMOOP26OJ3PU")
mental_health_facilities <- read.csv("https://raw.githubusercontent.com/Reed-Statistics/math241S20PostGrp4/master/mental_health_facilities.csv?token=ANDSWNMOS4SBJG3DTIMUUYK6OJ3QM")
suicide_death_rates <- read.csv("https://raw.githubusercontent.com/Reed-Statistics/math241S20PostGrp4/master/suicide-death-rates.csv?token=ANDSWNLHXALZJ4XT3PTCI6S6OJ3SU")
lower_bound <- rep(NA, 158)
upper_bound <- rep(NA, 158)

happiness_2015 <- happiness_2015 %>%
  mutate(year = 2015) %>%
  select(1, 3, 4, 6:11, 13) %>%
  mutate(lower_bound = lower_bound, upper_bound = upper_bound) %>%
  rename(country = 1,
         happiness_rank = 2,
         happiness_score = 3,
         gdp = 4,
         family = 5,
         life_expectancy = 6,
         freedom = 7,
         trust_corruption = 8,
         generosity = 9,
         year = 10,
         lower_whisker = 11,
         upper_whisker = 12)

happiness_2016 <- happiness_2016 %>%
  mutate(year = 2016) %>%
  select(1, 3, 4:12, 14) %>%
  rename(country = 1,
         happiness_rank = 2,
         happiness_score = 3,
         gdp = 6,
         family = 7,
         life_expectancy = 8,
         freedom = 9,
         trust_corruption = 10,
         generosity = 11,
         year = 12,
         lower_whisker = 4,
         upper_whisker = 5)
  

happiness_2017 <- happiness_2017 %>%
  mutate(year = 2017) %>%
  select(-12) %>%
  rename(country = 1,
         happiness_rank = 2,
         happiness_score = 3,
         gdp = 6,
         family = 7,
         life_expectancy = 8,
         freedom = 9,
         trust_corruption = 10,
         generosity = 11,
         year = 12,
         lower_whisker = 4,
         upper_whisker = 5)

lower_bound <- rep(NA, 156)
upper_bound <- rep(NA, 156)

happiness_2018 <- happiness_2018 %>%
  mutate(year = 2018) %>%
  mutate(lower_bound = lower_bound, upper_bound = upper_bound) %>%
  rename(country = 2,
         happiness_rank = 1,
         happiness_score = 3,
         gdp = 4,
         family = 5,
         life_expectancy = 6,
         freedom = 7,
         trust_corruption = 9,
         generosity = 8,
         year = 10,
         lower_whisker = 11,
         upper_whisker = 12)

happiness_2019 <- happiness_2019 %>%
  mutate(year = 2019)  %>%
  mutate(lower_bound = lower_bound, upper_bound = upper_bound) %>%
  rename(country = 2,
         happiness_rank = 1,
         happiness_score = 3,
         gdp = 4,
         family = 5,
         life_expectancy = 6,
         freedom = 7,
         trust_corruption = 9,
         generosity = 8,
         year = 10,
         lower_whisker = 11,
         upper_whisker = 12)
  
happiness <- do.call("rbind", list(happiness_2015, happiness_2016, happiness_2017, happiness_2018, happiness_2019))
suicide_death_rates <- suicide_death_rates %>%
  select(-2) %>%
  rename(sdr = 3)

countries <- happiness %>%
  left_join(mental_health_facilities, by = c("country" = "Country")) %>%
  left_join(suicide_death_rates, by = c(c("country" = "Entity"),
                                        c("year" = "Year"))) %>%
  select(-13)
## Warning: Column `country`/`Country` joining factors with different levels,
## coercing to character vector
## Warning: Column `country`/`Entity` joining character vector and factor, coercing
## into character vector
# retrieve the world map data
world_map <- map_data("world")
# merge map and countries data
countries_map <- left_join(countries, world_map, by = c("country" = "region"))
# create the static map colored with happiness scores
happiness_map <- ggplot(countries_map, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = happiness_score), colour = "white") +
  scale_fill_viridis(direction = 1, option = "B") +
  theme_void() +
  theme(legend.direction = "vertical", 
        legend.title.align = 0,
        legend.key.size = unit(0.5, "cm"),
        legend.title=element_text(size = 10), 
        legend.text=element_text(size = 6)) +
  labs(fill = "happiness score")
# create an animated map (unfinished, have to think about how to handle missing values)
# maybe add more years???
happiness_map +
  transition_manual(year)
## nframes and fps adjusted to match transition

countries$continent <- countrycode(sourcevar = countries[, "country"],
                            origin = "country.name",
                            destination = "continent") 
## Warning in countrycode(sourcevar = countries[, "country"], origin = "country.name", : Some values were not matched unambiguously: Kosovo
# manually assign the continent name to Kosovo
countries[countries$country =="Kosovo", "continent"] <- "Europe"

# create a scatterplot to visualize the relationship between happiness and suicide

suicide_happiness <- countries %>% ggplot(aes(x = happiness_score, y = sdr, 
                                           color = continent, frame = year, ids = country)) +
  geom_point(aes(size = life_expectancy), alpha = 0.7) +
  scale_x_log10() +
  labs(x = "Happiness Score", y = "Suicide death rate per 100,000",
       size = NULL, color = "Continent") +
  theme_minimal() 

ggplotly(suicide_happiness)